<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\tree_table\util\tttfilter;

use builder\helper\Url;
use yii\base\InvalidArgumentException;

/**
 * 下拉联动控件
 * @author cleverstone
 * @since ym2.0
 */
class SelectLink implements FilterControlInterface
{
    /**
     * @var array 选项
     */
    private $_options = [
        'control' => 'select_link', // 控件类型
        'label' => '',    // 标签
        'placeholder' => '请选择', // 提示
        'default' => '',    // 默认值, 只有start下拉可以设置;
        'class' => '',      // ui类
        'style' => '',      // 样式
        'attribute' => '',  // 属性
        'options' => [],    // 选项
        'targetLink' => '', // 目标连接字段
        'route' => '',      // 路由
        'disabled' => [],//禁用项
        'uiConfig' => [
            'allowClear' => true,//允许清除选中
            'width' => '100%',//控件宽
            'selectedInOrder' => true, //多选时,是否按选择顺序排序
            'selectedHideOption' => true, //多选时,是否隐藏已选择过的选项
            'closeOnSelect' => true, //选择后,是否关闭选项内容
        ],
    ];

    /**
     * 选择后,不关闭选项内容
     * @return $this
     */
    public function unCloseOnSelect()
    {
        $this->_options['uiConfig']['closeOnSelect'] = false;
        return $this;
    }

    /**
     * select2 配置
     * @param string $key 配置
     * @param mixed $value 值
     * @return $this
     */
    public function uiConfig($key, $value)
    {
        $this->_options['uiConfig'][$key] = $value;
        return $this;
    }

    /**
     * 禁用项(start可用)
     * @param array $item
     * @return $this
     */
    public function disabled(array $item)
    {
        $this->_options['disabled'] = $item;
        return $this;
    }

    /**
     * @param string $label 标签
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = (string)$label;
        return $this;
    }

    /**
     * @param string $placeholder 内容提示
     * @return $this
     */
    public function placeholder($placeholder)
    {
        $this->_options['placeholder'] = (string)$placeholder;
        return $this;
    }

    /**
     * 默认值
     * @param string $default
     * @return $this
     */
    public function defaultValue($default = '')
    {
        $this->_options['default'] = (string)$default;
        return $this;
    }

    /**
     * @param string|array $class CSS类
     * @return $this
     */
    public function uiClass($class)
    {
        $this->_options['class'] = $class;
        return $this;
    }

    /**
     * @param string|array $style CSS样式
     * @return $this
     */
    public function style($style)
    {
        $this->_options['style'] = $style;
        return $this;
    }

    /**
     * @param array|string $attribute HTML标签属性
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->_options['attribute'] = $attribute;
        return $this;
    }

    /**
     * @param array $options 初始选项
     * @return $this
     */
    public function initOptions(array $options)
    {
        $this->_options['options'] = $options;
        return $this;
    }

    /**
     * @param string $targetLinkField 目标连接字段
     * @return $this
     */
    public function targetLink($targetLinkField)
    {
        $this->_options['targetLink'] = (string)$targetLinkField;
        return $this;
    }

    /**
     * @param string|array $route 路由
     * ```php
     * // 响应示例1:
     *  return $this->asOk('success', [
     *      ['value' => 'D', 'text' => '郑州市', 'disabled' => true],
     *      ['value' => 'E', 'text' => '东莞市'],
     *      ['value' => 'F', 'text' => '北京市'],
     *  ]);
     *
     * // 响应示例2:
     *  return $this->asOk('success', [
     *      '中国' => [
     *          ['value' => 'D', 'text' => '郑州市', 'disabled' => true],
     *          ['value' => 'E', 'text' => '东莞市'],
     *          ['value' => 'F', 'text' => '北京市'],
     *      ],
     *      '美国' => [
     *          ['value' => 'E', 'text' => '东莞市'],
     *          ['value' => 'F', 'text' => '北京市'],
     *      ],
     *  ]);
     *
     * ```
     * - value 值
     * - text 文本
     * - disabled 是否禁用
     * @return $this
     */
    public function route($route)
    {
        $this->_options['route'] = Url::to($route, '');
        return $this;
    }

    /**
     * 连接开始
     * @return $this
     */
    public function start()
    {
        if (
            empty($this->_options['options'])
            || empty($this->_options['targetLink'])
            || empty($this->_options['route'])
        ) {
            throw new InvalidArgumentException('请检查是否已初始化联动选择项, 设置联动连接字段和请求路由!');
        }

        return $this;
    }

    /**
     * 连接项
     * @return $this
     */
    public function through()
    {
        $this->_options['options'] = [];
        $this->_options['default'] = '';

        if (
            empty($this->_options['targetLink'])
            || empty($this->_options['route'])
        ) {
            throw new InvalidArgumentException('请检查是否已设置联动连接字段和请求路由!');
        }

        return $this;
    }

    /**
     * 连接结束
     * @return $this
     */
    public function end()
    {
        $this->_options['options'] = [];
        $this->_options['targetLink'] = '';
        $this->_options['route'] = '';
        $this->_options['default'] = '';

        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        return $this->_options;
    }
}